package com.itheima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;

@Slf4j
@Configuration
public class MyMetaObjecthandler implements MetaObjectHandler
{
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info(metaObject.toString());
        if (metaObject.getOriginalObject() instanceof Orders) {
            metaObject.setValue("number", String.valueOf(IdWorker.getId()));
        } else {
            metaObject.setValue("createTime", LocalDateTime.now());
            metaObject.setValue("updateTime", LocalDateTime.now());

            metaObject.setValue("createUser", BaseContext.getCurrentId());
            metaObject.setValue("updateUser",BaseContext.getCurrentId());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info(metaObject.toString());

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }
}
